# http://www.cis.upenn.edu/~xtag/
# 处理动词的各种变形信息

import os
import re
import csv

count = 0
def parse_db_file(filename):
    global count

    f = open(filename, 'r' ,encoding='utf8')
    fo = open("e:\\not_handled.txt", 'w+' ,encoding='utf8')
    f_result = open(filename+".out", 'w+' ,encoding='utf8')

    fo.write(filename+"\n")

    word_list = []

    for line in f.readlines()[4:]:
        m = re.search("^[{](?P<rest>[^(]+)(?P<mean>[(].+[)])\s*[}]\s*$", line)
        if m:
            # 注释
            #fo.write(m.group("mean")+"\n")
            #
            #print(line)
            meaning = m.group("mean")
            count  = count + 1

            newitem = None

            rest = m.group("rest")
            pos = 0
            matched = True
            while pos < len(rest) and matched == True:
                matched = False
                #print(line)
                n = re.compile("\s*\[([^]]+)\]").match(rest, pos)
                if n:
                    #print("==="+n.group(1))
                    ss_newitem = None
                    ss_matched = True
                    ss = n.group(1)
                    ss_pos = 0
                    while ss_pos < len(ss) and ss_matched == True:
                        ss_matched = False
                        ss_n = re.compile("\s*(noun[.][a-zA-Z0-9:.,@$>+\^]+[^ ]*|adv[.][a-zA-Z0-9:.,@$>+\^]+[^ ]*|adj[.][a-zA-Z0-9:.,@$>+\^]+[^ ]*|verb[.][a-zA-Z0-9:.,@$>+\^]+[^ ]*)\s*(?!noun[.]|adv[.]|adj[.]|verb[.])").match(ss, ss_pos)
                        if ss_n:
                            #print("**"+ss_n.group())
                            if ss_newitem is None:
                                ss_newitem = dict()
                                ss_newitem['name'] = ""
                                ss_newitem['mean'] = meaning
                                ss_newitem['pos'] = []
                                ss_newitem['seq'] = count
                                word_list.append(ss_newitem)
                                #print("**"+n.group())
                            ss_newitem['pos'].append(ss_n.group())
                            
                            ss_pos = ss_n.end()
                            ss_matched = True

                        ss_n = re.compile("\s*frames:\s*[0-9 ,]+\s*").match(ss, ss_pos)
                        if ss_n:
                            #print("--"+ss_n.group())
                            ss_pos = ss_n.end()
                            ss_matched = True

                        ss_n = re.compile("\s*[a-zA-Z0-9_'-]+[,][^ ]*\s*").match(ss, ss_pos)
                        if ss_n:
                            #print("=="+ss_n.group())
                            ss_newitem = dict()
                            ss_newitem['name'] = ss_n.group()
                            ss_newitem['mean'] = meaning
                            ss_newitem['pos'] = []
                            ss_newitem['seq'] = count
                            word_list.append(ss_newitem)
                    
                            ss_pos = ss_n.end()
                            ss_matched = True

                        ss_n = re.compile("\s+").match(ss, ss_pos)
                        if ss_n:
                            ss_pos = ss_n.end()
                            ss_matched = True

                    if len(ss[ss_pos:]): fo.write("???? "+ss[ss_pos:]+"\n")


                    pos = n.end()
                    matched = True

                n = re.compile("\s*(noun[.][a-zA-Z0-9:.,@$>\^]+[^ ]*|adv[.][a-zA-Z0-9:.,@$>\^]+[^ ]*|adj[.][a-zA-Z0-9:.,@$>\^]+[^ ]*|verb[.][a-zA-Z0-9:.,@$>\^]+[^ ]*)(?!noun[.]|adv[.]|adj[.]|verb[.])").match(rest, pos)
                if n:
                    #
                    if newitem is None:
                        newitem = dict()
                        newitem['name'] = ""
                        newitem['mean'] = meaning
                        newitem['pos'] = []
                        newitem['seq'] = count
                        word_list.append(newitem)
                        #print("**"+n.group())
                    newitem['pos'].append(n.group())

                    pos = n.end()
                    matched = True

                n = re.compile("\s*frames:\s*[0-9 ,]+").match(rest, pos)
                if n:
                    #print("**"+n.group())
                    pos = n.end()
                    matched = True

                n = re.compile("\s*[a-zA-Z0-9_'-]+[,][^ ]*").match(rest, pos)
                if n:
                    #print("**"+n.group())
                    newitem = dict()
                    newitem['name'] = n.group()
                    newitem['mean'] = meaning
                    newitem['pos'] = []
                    newitem['seq'] = count
                    word_list.append(newitem)
                    pos = n.end()
                    matched = True

                n = re.compile("\s+").match(rest, pos)
                if n:
                    pos = n.end()
                    matched = True

            if len(rest[pos:]):  fo.write(rest[pos:]+"\n")

    for word in word_list:
        #print((word['name'] or "") + "|"+",".join(p for p in word['pos'])+"|" + newitem['mean'] )
        f_result.write(str(word['seq'])+"|"+(word['name'] or "").strip() + "|"+",".join(p.strip() for p in word['pos'])+"|" + word['mean'] +"\n")
        pass

    f.close()
    fo.close()
    f_result.close()

#===================================================================
files = [
    "verb.body",
    "verb.change",
    "verb.cognition",
    "verb.communication",
    "verb.competition",
    "verb.consumption",
    "verb.contact",
    "verb.creation",
    "verb.emotion",
    "verb.motion",
    "verb.perception",
    "verb.possession",
    "verb.social",
    "verb.stative",
    "verb.weather",
#    "adj.all",
#    "adj.pert",
#    "adj.ppl",
#    "adv.all",
#    "noun.Tops",
#    "noun.act",
#    "noun.animal",
#    "noun.artifact",
#    "noun.attribute",
#    "noun.body",
#    "noun.cognition",
#    "noun.communication",
#    "noun.event",
#    "noun.feeling",
#    "noun.food",
#    "noun.group",
#    "noun.location",
#    "noun.motive",
#    "noun.object",
#    "noun.person",
#    "noun.phenomenon",
#    "noun.plant",
#    "noun.possession",
#    "noun.process",
#    "noun.quantity",
#    "noun.relation",
#    "noun.shape",
#    "noun.state",
#    "noun.substance",
#    "noun.time",
]

for file in files:
    parse_db_file("E:\\W\\"+file)

